<template>
  <div class="login-wrap">
    <el-row type="flex" class="row-bg row-wrap" justify="space-around">
      <el-col :md="8" class="left-wrap">
      </el-col>
      <el-col :md="10" class="right-wrap">
        <el-form :model="loginForm" :rules="fieldRules" ref="loginForm" size="medium" class="login-container">
          <el-col class="login-logo" :md="12">

          </el-col>
          <el-col :md="11">
            <el-form-item prop="account">
              <el-input type="text" v-model="loginForm.account" autofocus="true" auto-complete="off" placeholder="账号"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input type="password" v-model="loginForm.password" auto-complete="off" placeholder="密码"></el-input>
            </el-form-item>
            <el-form-item>
              <el-col :span="11">
                <el-form-item prop="captcha">
                  <el-input type="test" v-model="loginForm.captcha" auto-complete="off" placeholder="验证码" style="width: 100%;">
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="11" :offset="2">
                <el-form-item>
                  <img style="width: 100%;" class="pointer" :src="loginForm.src" @click="refreshCaptcha">
                </el-form-item>
              </el-col>
            </el-form-item>
            <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox>
            <el-form-item style="width:100%;">
              <!-- <el-button type="primary" style="width:48%;" @click.native.prevent="reset">重 置</el-button> -->
              <el-button type="primary" round style="width:100%;" @click.native.prevent="login" :loading="loading">登 录</el-button>
            </el-form-item>
          </el-col>
          <!-- <span class="tool-bar"> -->
          <!-- 主题切换 -->
          <theme-picker style="float:right;display:none;" class="theme-picker" :default="themeColor" @onThemeChange="onThemeChange"></theme-picker>
          <!-- 语言切换 -->
          <!-- <lang-selector class="lang-selector"></lang-selector>    -->
          <!-- </span> -->
          <!-- <h2 class="title" style="padding-left:22px;">系统登录</h2> -->

        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import Cookies from "js-cookie"
import ThemePicker from "@/components/ThemePicker"
import LangSelector from "@/components/LangSelector"
export default {
  name: 'Login',
  components:{
    ThemePicker,
    LangSelector
  },
  data() {
    return {
      loading: false,
      loginForm: {
        account: 'admin',
        password: 'admin',
        captcha:'',
        src: ''
      },
      fieldRules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
        ,
        captcha: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      checked: true
    }
  },
  methods: {
    login() {
      this.loading = true
      let userInfo = {account:this.loginForm.account, password:this.loginForm.password, captcha:this.loginForm.captcha}
      this.$api.login.login(userInfo).then((res) => {
        console.log(res);
          if(res.msg != null) {
            this.$message({
              message: res.msg,
              type: 'error'
            })
          } else {
            Cookies.set('token', res.data.token) // 放置token到Cookie
            sessionStorage.setItem('user', userInfo.account) // 保存用户到本地会话
            // 当前用户userId
            this.$api.user.findByName({name: userInfo.account}).then(res => {
              sessionStorage.setItem('userId', res.data.id) // 保存用户到本地会话
            });
            this.$store.commit('menuRouteLoaded', false) // 要求重新加载导航菜单
            this.$router.push('/')  // 登录成功，跳转到主页
          }
          this.loading = false
        }).catch((res) => {
          this.$message({
          message: res.message,
          type: 'error'
          })
        });
    },
    refreshCaptcha: function(){
      this.loginForm.src = this.global.baseUrl + "/captcha.jpg?t=" + new Date().getTime();
    },
    reset() {
      this.$refs.loginForm.resetFields()
    },
    // 切换主题
    onThemeChange: function(themeColor) {
      this.$store.commit('setThemeColor', themeColor)
    }
  },
  mounted() {
    this.refreshCaptcha()
  },
  computed:{
    ...mapState({
      themeColor: state=>state.app.themeColor
    })
  }
}
</script>

<style lang="scss"scoped>.login-wrap {
  background: url(../assets/images/login-bg.jpg) center no-repeat;
  position: absolute;
  width: 100vw;
  height: 100vh;
  background-size: 100vw 100vh;

  .row-wrap {
    margin-top: calc((100vh - 350px)/2);
  }

  .left-wrap {
    background: url(../assets/images/login-left.png) center no-repeat;
    background-size: contain;
    max-width: 550px;
    margin-top: 170px;
    height: 100px;
  }

  .right-wrap {
    padding: 35px;
    max-width: 800px;
    background-color: rgba(255, 255, 255, 0.85);
    box-shadow: 0 0 10px rgba(61, 61, 61, 0.4);
  }

  .login-container {
    text-align: left;
    .login-logo {
      background: url(../assets/images/login-logo.png) left no-repeat;
      background-size: contain;
      height: 230px;
    }

    .title {
      margin: 0px auto 30px auto;
      text-align: center;
      color: #505458;
    }

    .remember {
      margin-bottom: 10px;
    }
  }
}

/deep/.el-form-item--medium .el-form-item__content {
    line-height: unset;
  }
  .el-form-item {
    margin-bottom: 20px;
  }
</style>